﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Command.BOMs;

namespace MyMES.Read.API.Application.Handler.BOMs
{
    public class BOMHandler : IRequestHandler<BOMcommand, APIPaging<List<BOM>>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<BOM> baseRepository;

        /// <summary>
        /// BOM展示
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="baseRepository"></param>
        public BOMHandler(IMapper mapper,IBaseRepository<BOM> baseRepository)
        {
            this.mapper = mapper;
            this.baseRepository = baseRepository;
        }
        /// <summary>
        /// BOM
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<APIPaging<List<BOM>>> Handle(BOMcommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIPaging<List<BOM>> result = new APIPaging<List<BOM>>();
                result.Code = APIEnum.查询成功;
                result.Msg = "显示成功";
                string sql = "select *  from BOM  ";
                var list = await baseRepository.SelectAsync(sql);
                result.TotalCount = list.Count;
                result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.size);
                list = list.Skip((request.index - 1) * request.size).Take(request.size).ToList();
                result.data = list;
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        
        }
    }
}
